使用POI实现JAVA操作Excel

您所在的位置:网站首页 poi写入excel 单元格入力规则 使用POI实现JAVA操作Excel

使用POI实现JAVA操作Excel

#使用POI实现JAVA操作Excel| 来源: 网络整理| 查看: 265

Apache POI POI提供API给JAVA程序对Microsoft Office格式档案读和写的功能

POI工具介绍

POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。主要是运用其中读取和输出excel的功能。

POI官网地址:

https://poi.apache.org/components/index.html

基本功能如下:

HSSF – 提供读写Excel格式(03)xls文件 XSSF – 提供读写Excel OOXML格式(07)xlsx文件 HWPF – 提供读写Word格式 HSLF – 提供读写PowerPoint格式 HDGF – 提供读写Visio格式 【注】03版本最多65535行,07版本的没有限制

可操作的文件类型

在这里插入图片描述

xls和xlsx的区别 xls是excel03版本 xlsx是excel07版本 最大的区别是行列数不同

xls最大支持65536行、256列 xlsx最大支持1048576行、16384列

poi操作

poi 操作xls poi-ooml操作xlsx

POI - Excel写

步骤

1、导入依赖 org.apache.poi poi 4.1.2 org.apache.poi poi-ooxml 4.1.2 joda-time joda-time 2.12.1 2、编写代码

4个主要步骤: 创建工作簿 – 创建工作表 – 创建行 – 创建列

2.1、少量数据 import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.joda.time.DateTime; /** * @author xxms */ public class ExcelWriteTest { public static void main(String[] args) throws Exception { POIExcel03(); POIExcel07(); } public static void POIExcel03() throws Exception { String PATH = "D:\\Study\\Back-end\\"; // 1、创建一个工作簿 03 Workbook workbook = new HSSFWorkbook(); // 2、创建一个工作表 Sheet sheet = workbook.createSheet("xxms观众统计表"); // 3、创建一个行 Row row1 = sheet.createRow(0); // 4、创建一个单元格 (1,1) Cell cell11 = row1.createCell(0); cell11.setCellValue("今日新增观众"); // (1,2) Cell cell12 = row1.createCell(1); cell12.setCellValue(666); // 第二行 Row row2 = sheet.createRow(1); //(2,1) Cell cell21 = row2.createCell(0); cell21.setCellValue("统计时间"); // (2,2) Cell cell22 = row2.createCell(1); String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); cell22.setCellValue(time); // 生成一张表(IO流) 03版本就是使用xls结尾 FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms观众统计表03.xls"); workbook.write(fileOutputStream); // 关闭流 fileOutputStream.close(); System.out.println("xxms观众统计表03 生成完毕"); } public static void POIExcel07() throws Exception { String PATH = "D:\\Study\\Back-end\\"; // 1、创建一个工作簿 07 Workbook workbook = new XSSFWorkbook(); // 2、创建一个工作表 Sheet sheet = workbook.createSheet("xxms观众统计表"); // 3、创建一个行 Row row1 = sheet.createRow(0); // 4、创建一个单元格 (1,1) Cell cell11 = row1.createCell(0); cell11.setCellValue("今日新增观众"); // (1,2) Cell cell12 = row1.createCell(1); cell12.setCellValue(666); // 第二行 Row row2 = sheet.createRow(1); //(2,1) Cell cell21 = row2.createCell(0); cell21.setCellValue("统计时间"); // (2,2) Cell cell22 = row2.createCell(1); String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss"); cell22.setCellValue(time); // 生成一张表(IO流) 07版本就是使用xlsx结尾 FileOutputStream fileOutputStream = new FileOutputStream(PATH+"xxms观众统计表07.xlsx"); workbook.write(fileOutputStream); // 关闭流 fileOutputStream.close(); System.out.println("xxms观众统计表07 生成完毕"); } } 2.2大数据量导入 package com.ruoyi.common.core.controller; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.joda.time.DateTime; /** * * * @author xxms */ public class ExcelWriteBigDataTest { public static void main(String[] args) throws Exception { POIExcel03BigData(); POIExcel07BigData(); } public static void POIExcel03BigData() throws Exception { //时间 long begin = System.currentTimeMillis(); String PATH = "D:\\Study\\Back-end\\"; // 1、创建一个工作簿 03 Workbook workbook = new HSSFWorkbook(); // 2、创建一个工作表 Sheet sheet = workbook.createSheet("xxms观众统计表"); // 3、写入数据 for(int rowNum=0;rowNum< 65536;rowNum++) { Row row = sheet.createRow(rowNum); for(int cellNum =0;cellNum


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3